package com.taotao.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.enums.OperateType;
import com.taotao.common.pojo.BaseResult;
import com.taotao.common.utils.Constants;
import com.taotao.common.utils.DateUtil;
import com.taotao.mapper.EyOperateLogMapper;
import com.taotao.pojo.EyOperateLog;
import com.taotao.pojo.EyOperateLogExample;
import com.taotao.service.OperateLogService;
import com.taotao.service.ParamService;

@Service
public class OperateLogServiceImpl implements OperateLogService {

	@Autowired
	EyOperateLogMapper eyOperateLogMapper;
	
	@Autowired
	ParamService paramService;
	
	BaseResult baseResult = new BaseResult();
	@Override
	public void insert(Integer memberId, Integer serviceId,Integer goodsId, String details,String operateType,String sessionKey) {
		String openOperateLog = paramService.getParam("openOperateLog");
		if("true".equals(openOperateLog)){
			EyOperateLog log=new EyOperateLog();
			log.setMemberId(memberId);
			log.setServiceId(serviceId);
			log.setGoodsId(goodsId);
			log.setOperateType(operateType);
			log.setDetails(details);
			log.setSessionKey(sessionKey);
			log.setCreateTime(DateUtil.getDateAndTime());
			eyOperateLogMapper.insert(log);
		}
	}
	
	
	@Override
	public Map<String, Object> operateLogAdd(Map<String, String> map) {
		String details = String.valueOf(map.get("details"));
		Map<String,String> m=new HashMap<>();
		String memberId = map.get("memberId"); 
		String operateType=map.get("operateType");
		details=StringUtils.isNotBlank(details)?details:OperateType.getValue(operateType);
		this.insert(Integer.parseInt(memberId), null,null,details,operateType,""); 
		
		return baseResult.getSuccMap();
	}
	
	
	
	@Override
	public Map<String, Object> queryOperateList(Map<String, String> map) {
		try {
			String pageNum =  map.get("pageNum");
			String pageSize = map.get("pageSize");
			String memberId =  map.get("memberId");
			String operateType =  map.get("operateType");
			Integer num=StringUtils.isBlank(pageNum)?Constants.pageNum:Integer.parseInt(pageNum);
			Integer size=StringUtils.isBlank(pageSize)?Constants.pageSize:Integer.parseInt(pageSize);
			
			EyOperateLogExample example = new EyOperateLogExample();
			com.taotao.pojo.EyOperateLogExample.Criteria criteria = example.createCriteria();
			if(StringUtils.isNotBlank(operateType)){
				criteria.andOperateTypeEqualTo(operateType);				
			}
			if(StringUtils.isNotBlank(memberId)){
				criteria.andMemberIdEqualTo(Integer.parseInt(memberId));
			}
			
			example.setOrderByClause(" create_time desc ");
			PageHelper.startPage(num,size);
			List<EyOperateLog> list = eyOperateLogMapper.selectByExample(example);
			for(EyOperateLog log:list){
				String createTime=DateUtil.getDateTimeFormatStr(log.getCreateTime());
				log.setCreateTime(createTime);
			}
			
			PageInfo<EyOperateLog> pageInfo = new PageInfo<>(list);
	  		return baseResult.getSuccMap(pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorJsonObj("网络繁忙，请稍后再试");
		}
	}
	
	
}
